package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.mapper.WsdSchoolsMapper;
import com.ruoyi.business.service.IWsdRoleClassService;
import com.ruoyi.business.service.IWsdSchoolsService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.SchoolTree.SchoolGradeClassVO;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.WsdRoleClass;
import com.ruoyi.common.core.domain.entity.WsdSchools;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;


/**
 * 学校信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-14
 */
@Service
public class WsdSchoolsServiceImpl implements IWsdSchoolsService
{
    @Autowired
    private WsdSchoolsMapper wsdSchoolsMapper;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询学校信息
     * 
     * @param id 学校信息主键
     * @return 学校信息
     */
    @Override
    public WsdSchools selectWsdSchoolsById(Long id) {
        return wsdSchoolsMapper.selectWsdSchoolsById(id);
    }

    /**
     * 查询学校信息列表
     * 
     * @param wsdSchools 学校信息
     * @return 学校信息
     */
    @Override
    public List<WsdSchools> selectWsdSchoolsList(WsdSchools wsdSchools) {
        return wsdSchoolsMapper.selectWsdSchoolsList(wsdSchools);
    }

    /**
     * 新增学校信息
     * 
     * @param wsdSchools 学校信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWsdSchools(WsdSchools wsdSchools) {
        //新增学校
        wsdSchoolsMapper.insertWsdSchools(wsdSchools);
        //新增用户
        Long id = wsdSchools.getId();
        SysUser user = new SysUser();
        user.setSchoolId(id);
        user.setUserName(Constants.NAME + id);
        user.setNickName(Constants.NAME + id);
        user.setPassword(SecurityUtils.encryptPassword(Constants.PASS_WORD));
        //绑定角色
        int i = userService.insertUser(user);
        userService.insertUserAuth(user.getUserId(),new Long[]{100L});
         return i ;
    }

    /**
     * 修改学校信息
     * 
     * @param wsdSchools 学校信息
     * @return 结果
     */
    @Override
    public int updateWsdSchools(WsdSchools wsdSchools)
    {
        wsdSchools.setUpdateTime(DateUtils.getNowDate());
        return wsdSchoolsMapper.updateWsdSchools(wsdSchools);
    }

    /**
     * 批量删除学校信息
     * 
     * @param ids 需要删除的学校信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdSchoolsByIds(Long[] ids)
    {
        return wsdSchoolsMapper.deleteWsdSchoolsByIds(ids);
    }

    /**
     * 删除学校信息信息
     * 
     * @param id 学校信息主键
     * @return 结果
     */
    @Override
    public int deleteWsdSchoolsById(Long id)
    {
        return wsdSchoolsMapper.deleteWsdSchoolsById(id);
    }

    /**
     * 构建学校树状图
     * @return
     */
    @Override
    public List<SchoolGradeClassVO> getSchoolTree(Long schoolId) {
        return wsdSchoolsMapper.getSchoolTree(schoolId);
    }

    @Override
    public List<SchoolGradeClassVO> selectClassesByRoleId(Long roleId, Long schoolId) {
        return wsdSchoolsMapper.selectClassesByRoleId(roleId,schoolId);
    }
}
